﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;
using System.Text.RegularExpressions;
using System.Data.OleDb;
 
namespace LCWMS
{
    public partial class w_mst003 : DockContent
    {
        private DataSet ds_transaction;
        private DataSet dscustomer;
        private int coint = 0;
          
        public w_mst003()
        {
            InitializeComponent();
            dgv_customer.AutoGenerateColumns = false;
        }

        /// <summary>
        /// 清除容器里面某些控件的值
        /// </summary>
        /// <param name="parContainer">容器类控件</param>
        public void ClearCntrValue(Control parContainer,bool bol,string edit)
        {
            for (int index = 0; index < parContainer.Controls.Count; index++)
            {
                // 如果是容器类控件，递归调用自己
                if (parContainer.Controls[index].HasChildren)
                {
                   // ClearCntrValue(parContainer.Controls[index]);
                }
                else
                {
                    switch (parContainer.Controls[index].GetType().Name)
                    {
                        
                        case "TextBox":
                            if (edit != "1")
                            {
                                parContainer.Controls[index].Text = "";
                            }
                            if (bol==false)
                            ((TextBox)parContainer.Controls[index]).ReadOnly = true;
                            else
                            ((TextBox)parContainer.Controls[index]).ReadOnly = false;
                            break;
                        case "RadioButton":
                            if (edit != "1")
                            ((RadioButton)(parContainer.Controls[index])).Checked = false;
                            ((RadioButton)(parContainer.Controls[index])).Enabled=bol;
                            break;
                        case "CheckBox":
                            if (edit != "1")
                            ((CheckBox)(parContainer.Controls[index])).Checked = false;
                            ((CheckBox)(parContainer.Controls[index])).Enabled = bol;
                            break;
                        case "ComboBox":
                            if (edit != "1")
                                ((ComboBox)(parContainer.Controls[index])).Text = "";
                            ((ComboBox)(parContainer.Controls[index])).Enabled = bol;
                            break;
                        case "DateTimePicker":
                            if (edit != "1")
                            ((DateTimePicker)(parContainer.Controls[index])).Text = "";
                            ((DateTimePicker)(parContainer.Controls[index])).Enabled = bol;
                            break;
                    }
                }
            }
            if (edit == "1")
            {
                this.tb_consignor.Enabled = false;
              
            }
            if (edit != "1")
            {
                cbx_consignor_flag.Text = "Non-owner";
                cbx_customer_flag.Text = "Non-clients";
                cbx_supply_flag.Text = "Non-supply";
            }
        }
        //初始化商品上架规则
        private void f_initaputawayrule()
        {
            DataSet f_dsku;
            string str_temp;
            //首先清空
            cbx_putawayrule.Text = "";
            str_temp = " select putaway_rule,rule_description  from " + main.g_space + ".mst_pa_rule where inv_org = '" + main.g_org + "' order by putaway_rule";
            try
            {
                if (main.dbtype == "SQL")
                    f_dsku = main.dbservice.Sql_exec_selectsql(str_temp);
                else
                    f_dsku = main.dbservice.oracle_exec_selectsql(str_temp);
                DataRow drnew;
                drnew = f_dsku.Tables[0].NewRow();
                drnew["putaway_rule"] = "";
                f_dsku.Tables[0].Rows.Add(drnew);
                cbx_putawayrule.DataSource = f_dsku.Tables[0].DefaultView;
                cbx_putawayrule.ValueMember = "putaway_rule";
                cbx_putawayrule.DisplayMember = "rule_description";
                cbx_putawayrule.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of allocate_rule", main.g_info_dataset) + "!" + ee.ToString());
            }
        }
        //初始化商品分配规则
        private void f_initallocaterule()
        {
            string str_temp;
            DataSet f_dsku;
            //首先清空
            cbx_allocaterule.Text = "";
            str_temp = " select allocate_rule,rule_description from " + main.g_space + ".mst_pk_rule where inv_org = '" + main.g_org + "' order by allocate_rule";
            try
            {
                if (main.dbtype == "SQL")
                    f_dsku = main.dbservice.Sql_exec_selectsql(str_temp);
                else
                    f_dsku = main.dbservice.oracle_exec_selectsql(str_temp);
                DataRow drnew;
                drnew = f_dsku.Tables[0].NewRow();
                drnew["allocate_rule"] = "";
                f_dsku.Tables[0].Rows.Add(drnew);
                cbx_allocaterule.DataSource = f_dsku.Tables[0].DefaultView;
                cbx_allocaterule.ValueMember = "allocate_rule";
                cbx_allocaterule.DisplayMember = "rule_description";
                cbx_allocaterule.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of allocate rule", main.g_info_dataset) + "!" + ee.ToString());
            }
        }
        //初始化商品批属性
        private void f_initallotno()
        {
            string str_temp;
            DataSet f_dsku;
            //首先清空
            cbx_lotno.Text = "";
            str_temp = " select lotno,lot_description from " + main.g_space + ".mst_attribute where inv_org = '" + main.g_org + "' order by lotno";
            try
            {
                if (main.dbtype == "SQL")
                    f_dsku = main.dbservice.Sql_exec_selectsql(str_temp);
                else
                    f_dsku = main.dbservice.oracle_exec_selectsql(str_temp);
                DataRow drnew;
                drnew = f_dsku.Tables[0].NewRow();
                drnew["lotno"] = "";
                f_dsku.Tables[0].Rows.Add(drnew);
                cbx_lotno.DataSource = f_dsku.Tables[0].DefaultView;
                cbx_lotno.ValueMember = "lotno";
                cbx_lotno.DisplayMember = "lot_description";
                cbx_lotno.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of batch attribute", main.g_info_dataset) + "!" + ee.ToString());
            }
        }
        //设置按钮可用和不可用,b_tmp 0说明可以编辑，否处于待保存状态
        private void f_setbutton(bool b_tmp)
        {
            if (b_tmp)//打开窗口
            {
                btn_new.Enabled = true;
                btn_modi.Enabled = true;
                btn_del.Enabled = true;
                dgv_customer.Enabled = true;
                btn_save.Enabled = false;
                btn_cancel.Enabled = false;

            }
            else //新增,修改
            {
                btn_new.Enabled = false;
                btn_modi.Enabled = false;
                btn_del.Enabled = false;
                dgv_customer.Enabled = false;
                btn_save.Enabled = true;
                btn_cancel.Enabled = true;
            }
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            coint = 1;
            f_setbutton(false);
            ClearCntrValue(this.groupBox3,true,"0");
            tb_consignor.Enabled = true;
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            string str_sql_where = "  ";
            string str_sql = "";

            if (this.rb_consignor_flag.Checked) 
            {
                str_sql_where = " and mst_1.consignor_flag='1'";
            }
            if (this.rb_customer_flag.Checked)
            {
                str_sql_where = " and mst_1.customer_flag='1'";
            }
            if (this.rb_supply_flag.Checked)
            {
                str_sql_where = " and mst_1.supply_flag in ('1','2')";
            }
            if (tb_cusname.Text.Trim() != "")
            {
                str_sql_where = " and mst_1.consignor_name like '%" + tb_cusname.Text.Trim() + "%'";
            }
            if (cbx_customer.Text != "")
            {
                str_sql += "select mst_1.* from " + main.g_space + ".mst_customer mst_1 where mst_1.inv_org = '" + main.g_org + "' and mst_1.consignor='" + cbx_customer.Text.Trim() + "' and mst_1.main_dist <> 'D'" + str_sql_where;
            }
            else
            {
                if (this.rb_consignor_flag.Checked)
                 str_sql += " select mst_1.* from " + main.g_space + ".mst_customer mst_1," + main.g_space + ".sys_user mst_2," + main.g_space + ".sys_user_customer mst_3" +
                             " where mst_2.inv_org = mst_3.inv_org and mst_2.user_id = mst_3.user_id and mst_3.inv_org = mst_1.inv_org and mst_3.consignor = mst_1.consignor and mst_2.inv_org = '" + main.g_org + "' and mst_2.user_id = '" + main.g_user + "' and mst_1.main_dist <> 'D' " + str_sql_where;
                else
                 str_sql += " select mst_1.* from " + main.g_space + ".mst_customer mst_1 where  mst_1.inv_org = '" + main.g_org + "' and mst_1.main_dist <> 'D' " + str_sql_where;
                           
            }

            ds_transaction = new DataSet();
            try
            {
                if (main.dbtype == "SQL")
                {
                    ds_transaction = main.dbservice.Sql_exec_selectsql(str_sql);

                }
                else
                {
                    ds_transaction = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                dgv_customer.DataSource = ds_transaction.Tables[0];
                if (dgv_customer.RowCount > 0)
                {
                    DataRow[] tr = ds_transaction.Tables[0].Select("consignor='" + dgv_customer.Rows[0].Cells["consignor"].Value.ToString() + "'");
                    show_all(tr[0]);
                }
                else
                ClearCntrValue(this.groupBox3, false, "0");
            }
            catch (System.Exception ee)
            {

                MessageBox.Show(main.g_class.comm_getchangestring("Fail to Query", main.g_info_dataset) + "!" + ee.ToString());
            }

        }

        private void w_mst003_Load(object sender, EventArgs e)
        {
            
            DataSet ds_pay_way = new DataSet();
            
            string str_common = "";

            //获取按钮权限
            string str_right;

            str_right = main.g_class.comm_getright("w_mst003", main.g_right_dataset);

            //新增

            if (str_right.Substring(0, 1) == "0")
            {
                btn_new.Visible = false;
            }
            else
            {
                btn_new.Visible = true;
            }
            //修改

            if (str_right.Substring(1, 1) == "0")
            {
                btn_modi.Visible = false;
            }

            else
            {
                btn_modi.Visible = true;
            }

            //删除

            if (str_right.Substring(2, 1) == "0")
            {
                btn_del.Visible = false;
            }

            else
            {
                btn_del.Visible = true;
            }

            //保存，取消

            if (str_right.Substring(3, 1) == "0")
            {
                btn_save.Visible = false;

                btn_cancel.Visible = false;
            }

            else
            {

                btn_save.Visible = true;

                btn_cancel.Visible = true;

            }

            rb_consignor_flag_CheckedChanged(sender, new EventArgs());

            try
            {
               

                if (main.dbtype == "SQL")
                {
                    str_common = "select dictionary_code,case use_flag when '1' then content1 when '2' then content2 when '3' then convert(char,number1) else convert(char,number2) end as transaction_typ from " + main.g_space + ".sys_dictionary where inv_org = '" + main.g_org + "' and ltrim(rtrim(dictionary_name))='pay_way' order by dictionary_code";
                    ds_pay_way = main.dbservice.Sql_exec_selectsql(str_common);
                }
                else
                {
                    str_common = "select dictionary_code,case use_flag when '1' then content1 when '2' then content2 when '3' then to_char(number1) else to_char(number2) end as transaction_typ from " + main.g_space + ".sys_dictionary where inv_org = '" + main.g_org + "' and trim(dictionary_name)='pay_way' order by dictionary_code";
                    ds_pay_way = main.dbservice.oracle_exec_selectsql(str_common);
                }
                int i_ds;
                i_ds = ds_pay_way.Tables[0].Rows.Count;
                if (i_ds > 0)
                {
                    for (int i = 0; i < i_ds; i++)
                    {
                        ds_pay_way.Tables[0].Rows[i]["transaction_typ"] = main.g_class.comm_getchangestring(ds_pay_way.Tables[0].Rows[i]["transaction_typ"].ToString().Trim(), main.g_info_dataset);
                        ds_pay_way.Tables[0].Rows[i]["dictionary_code"] = main.g_class.comm_getchangestring(ds_pay_way.Tables[0].Rows[i]["dictionary_code"].ToString().Trim(), main.g_info_dataset);
                    }
                }
              

                DataRow dr = ds_pay_way.Tables[0].NewRow();
                dr["dictionary_code"] = "";
                dr["transaction_typ"] = "";
                ds_pay_way.Tables[0].Rows.Add(dr);
                cbx_pay_way.DataSource = ds_pay_way.Tables[0];
                cbx_pay_way.ValueMember = "dictionary_code";
                cbx_pay_way.DisplayMember = "transaction_typ";
                cbx_pay_way.Text = "";



                ClearCntrValue(this.groupBox3, false,"0");
                main.g_class.comm_changeControl(this, main.g_win_dataset);
                main.g_class.comm_changeCbxContent(this.cbx_supply_flag, main.g_info_dataset, '0');
                main.g_class.comm_changeCbxContent(this.cbx_consignor_flag, main.g_info_dataset, '0');
                main.g_class.comm_changeCbxContent(this.cbx_customer_flag, main.g_info_dataset, '0');
                main.g_class.comm_changeCbxContent(this.cbx_corporation_type, main.g_info_dataset, '0');
                main.g_class.comm_changeCbxContent(this.cbx_downtown, main.g_info_dataset, '0');
                
           }
           catch (System.Exception ee)

           {
               MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize paid way", main.g_info_dataset) + "!" + ee.ToString());
               return;
           }
           //初始化按钮
           f_setbutton(true);
           DataTable dt_con = new DataTable();
           dt_con.Columns.Add("consignor");
           dt_con.Columns.Add("consignor_name");
           dt_con.Columns.Add("short_name");

           dgv_customer.DataSource = dt_con;
           
        }

        private void dgv_customer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataSet ds_con;
           // DataRow[] tr = ds_transaction.Tables[0].Select("consignor='" + dgv_customer.Rows[dgv_customer.CurrentRow.Index].Cells["consignor"].Value.ToString() + "'");
            string  str_sql = "select * from " + main.g_space + ".mst_customer  where inv_org = '" + main.g_org + "' and consignor='" + dgv_customer.Rows[dgv_customer.CurrentRow.Index].Cells["consignor"].Value.ToString() + "' and main_dist <> 'D'" ;
            try
            {
                if (main.dbtype == "SQL")
                {
                    ds_con = main.dbservice.Sql_exec_selectsql(str_sql);

                }
                else
                {
                    ds_con = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                if (ds_con.Tables[0].Rows.Count>0)
                    show_all(ds_con.Tables[0].Rows[0]);
            }
            catch (System.Exception ee)  
            {
                MessageBox.Show(ee.Message.ToString().Trim());
            }
        }
     
        private void show_all(DataRow tr)
        {
            this.tb_consignor.Text = tr["consignor"].ToString().Trim();
            this.tb_consignor_name.Text = tr["consignor_name"].ToString().Trim();
            this.tb_short_name.Text = tr["short_name"].ToString().Trim();

            //this.tb_short_name.Text = tr["short_name"].ToString().Trim();
            this.tb_postalcode.Text = tr["postalcode"].ToString().Trim();
            this.tb_english_name.Text = tr["english_name"].ToString().Trim();

            this.tb_telephone.Text = tr["telephone"].ToString().Trim();
            this.tb_business_address.Text = tr["business_address"].ToString().Trim();
            this.tb_register_address.Text = tr["register_address"].ToString().Trim();

            this.tb_address3.Text = tr["address3"].ToString().Trim();
            if (tr["address4"].ToString().Trim() == "1")
            {
                checkBox1.Checked = true;
            }
            else
            {
                checkBox1.Checked = false;
            }
            this.tb_mobile_phone.Text = tr["mobile_phone"].ToString().Trim();

            this.tb_fax.Text = tr["fax"].ToString().Trim();
            this.tb_email.Text = tr["email"].ToString().Trim();
            this.tb_incorporation.Text = tr["incorporation"].ToString().Trim();

            this.tb_clerk.Text = tr["clerk"].ToString().Trim();
            this.tb_department.Text = tr["department"].ToString().Trim();
            this.cbx_pay_way.Text = tr["pay_way"].ToString().Trim();
            this.tb_pay_deadline.Text = tr["pay_deadline"].ToString().Trim();
            this.tb_pay_period.Text = tr["pay_period"].ToString().Trim();
            this.tb_bank.Text = tr["bank"].ToString().Trim();

            this.dtp_appoint_day.Text = tr["appoint_day"].ToString().Trim();
            this.tb_settlement_day.Text = tr["settlement_day"].ToString().Trim();
            this.tb_accounts.Text = tr["accounts"].ToString().Trim();

            this.tb_country.Text = tr["country"].ToString().Trim();
            this.tb_province.Text = tr["province"].ToString().Trim();
            this.tb_city.Text = tr["city"].ToString().Trim();

            switch (tr["downtown"].ToString().Trim())
            {
                case "0":
                    this.cbx_downtown.Text = main.g_class.comm_getchangestring("incity", main.g_info_dataset); 
                    break;
                case "1":
                    this.cbx_downtown.Text = main.g_class.comm_getchangestring("extramural", main.g_info_dataset); 
                    break;
            }
            switch (tr["corporation_type"].ToString().Trim())
            {
                case "0":
                    this.cbx_corporation_type.Text = main.g_class.comm_getchangestring("national", main.g_info_dataset); 
                    break;
                case "1":
                    this.cbx_corporation_type.Text = main.g_class.comm_getchangestring("joint-stock", main.g_info_dataset); 
                    break;
                case "2":
                    this.cbx_corporation_type.Text = main.g_class.comm_getchangestring("Private", main.g_info_dataset); 
                    break;
                case "3":
                    this.cbx_corporation_type.Text = main.g_class.comm_getchangestring("Foreign", main.g_info_dataset); 
                    break;
            }
            switch (tr["consignor_flag"].ToString().Trim())
            {
                case "0":
                    this.cbx_consignor_flag.Text = main.g_class.comm_getchangestring("Non-CSG", main.g_info_dataset); 
                    break;
                case "1":
                    this.cbx_consignor_flag.Text = main.g_class.comm_getchangestring("CSG", main.g_info_dataset); 
                    break;
            }
            switch (tr["customer_flag"].ToString().Trim())
            {
                case "0":
                    this.cbx_customer_flag.Text = main.g_class.comm_getchangestring("Non-client", main.g_info_dataset); 
                    break;
                case "1":
                    this.cbx_customer_flag.Text = main.g_class.comm_getchangestring("client", main.g_info_dataset); 
                    break;
            }
            switch (tr["supply_flag"].ToString().Trim())
            {
                case "0":
                    this.cbx_supply_flag.Text = main.g_class.comm_getchangestring("Non-supply", main.g_info_dataset); 
                    break;
                case "1":
                    this.cbx_supply_flag.Text = main.g_class.comm_getchangestring("supply", main.g_info_dataset); 
                    break;
                default:
                    this.cbx_supply_flag.Text = main.g_class.comm_getchangestring("Non-supply", main.g_info_dataset);
                    break;
            }
            this.tb_tax_no.Text = tr["tax_no"].ToString().Trim();
            this.cbx_putawayrule.SelectedValue = tr["putaway_rule"].ToString().Trim();
            this.cbx_allocaterule.SelectedValue = tr["allocate_rule"].ToString().Trim();
            this.cbx_lotno.SelectedValue = tr["lotno"].ToString().Trim();
            this.tb_memo.Text = tr["memo"].ToString().Trim();
        }

        private void btn_modi_Click(object sender, EventArgs e)
        {
            if (tb_consignor.Text.Trim() != "")
            {
                coint = 2;
                f_setbutton(false);

                ClearCntrValue(this.groupBox3, true, "1");
            }
            else
            {
                MessageBox.Show("必须选择一个货主或供应商！");
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
           
            f_setbutton(true);
            if (coint == 2)
            {
                ClearCntrValue(this.groupBox3, false, "1");
            }
            else
            {
                ClearCntrValue(this.groupBox3, false, "0");
            }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string str_user_customer = "";
            if (this.tb_consignor.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please choose a consignor", main.g_info_dataset) + "!");
                return;
            }

            string str_downtown = "";
            string str_corporation_type = "";
            string str_consignor_flag = "";
            string str_customer_flag = "";
            string str_supply_flag = "";
            string str_temp = "";
            string str_in_sql = "";
            string str_delegate_supp = "0";
            if (tb_consignor_name.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please input a consignor name ", main.g_info_dataset));
                return;
            }
            //市内市外区分
            str_downtown = main.g_class.comm_getconversestring(cbx_downtown.Text.Trim(), main.g_info_dataset);
            switch (str_downtown.Trim().ToLower())
            {
                case "incity":
                    str_downtown = "0";
                    break;
                case "extramural":
                    str_downtown = "1";
                    break;
                default:
                    str_downtown = "";
                    break;
            }

            //货主区分
            str_consignor_flag = main.g_class.comm_getconversestring(cbx_consignor_flag.Text.Trim(), main.g_info_dataset);
            switch (str_consignor_flag.Trim().ToLower())
            {
                case "non-csg":
                    str_consignor_flag = "0";
                    break;
                case "csg":
                    str_consignor_flag = "1";
                    break;
                default:
                    str_consignor_flag = "";
                    break;
            }
            //客户区分
            str_customer_flag = main.g_class.comm_getconversestring(cbx_customer_flag.Text.Trim(), main.g_info_dataset);
            switch (str_customer_flag.Trim().ToLower())
            {
                case "non-client":
                    str_customer_flag = "0";
                    break;
                case "client":
                    str_customer_flag = "1";
                    break;
                default:
                    str_customer_flag = "";
                    break;
            }
            //供应商区分
            str_supply_flag = main.g_class.comm_getconversestring(cbx_supply_flag.Text.Trim(), main.g_info_dataset);
            switch (str_supply_flag.Trim().ToLower())
            {
                case "non-supply":
                    str_supply_flag = "0";
                    break;
                case "supply":
                    str_supply_flag = "1";
                    break;
                default:
                    str_supply_flag = "0";
                    break;
            }
            //是否委外供应商
            if (checkBox1.Checked)
            {
                str_delegate_supp = "1";
            }
            else
            {
                str_delegate_supp = "0";
            }
            //企业类型区分
            str_corporation_type = main.g_class.comm_getconversestring(cbx_corporation_type.Text.Trim(), main.g_info_dataset);
            switch (str_corporation_type.Trim().ToLower())
            {
                case "national":
                    str_corporation_type = "0";
                    break;
                case "joint-stock":
                    str_corporation_type = "1";
                    break;
                case "private":
                    str_corporation_type = "2";
                    break;
                case "foreign":
                    str_corporation_type = "3";
                    break;
                default:
                    str_corporation_type = "";
                    break;
            }

            if (str_consignor_flag == "0" && str_customer_flag == "0" && str_supply_flag == "0")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("please choose one among consignor,customer and supplier at least", main.g_info_dataset) + "!");
                return;
            }
            //分配规则
            string str_allocaterule;
            if (cbx_allocaterule.Text.Trim() == "")
            {
                str_allocaterule = "";
            }
            else
            {
                str_allocaterule = this.cbx_allocaterule.SelectedValue.ToString().Trim();
            }
            //上架规则代码
            string str_putawayrule;
            if (cbx_putawayrule.Text.Trim() == "")
            {
                str_putawayrule = "";
            }
            else
            {
                str_putawayrule = this.cbx_putawayrule.SelectedValue.ToString().Trim();
            }
            //批次属性
            string str_lotno;
            if (cbx_lotno.Text.Trim() == "")
            {
                str_lotno = "";
            }
            else
            {
                str_lotno = this.cbx_lotno.SelectedValue.ToString().Trim();
            }

            if (coint == 1)//添加
            {
                string str_pk = "select count(*) from " + main.g_space + ".mst_customer where inv_org = '" + main.g_org + "' and consignor='" + this.tb_consignor.Text.Trim() + "'";
                str_user_customer = "insert into  " + main.g_space + ".sys_user_customer(" +
                    "inv_org,user_id,consignor,verify_code,main_dist,made_time,maker)" +
                    "values('" + main.g_org + "','" + main.g_user + "','" + this.tb_consignor.Text.Trim() + "','','A',";

                int dount = -1;
                if (main.dbtype == "SQL")
                {
                    dount = main.dbservice.Sql_exec_selectint(str_pk);
                    str_temp = str_temp + "getdate(),'";
                }
                else
                {
                    dount = main.dbservice.oracle_exec_selectint(str_pk);
                    str_temp = str_temp + " sysdate,'";
                }
                if (dount > 0)
                {
                    MessageBox.Show(main.g_class.comm_getchangestring("consignor exists", main.g_info_dataset) + "!");
                    this.tb_consignor.Focus();
                    return;
                }
                str_temp = str_temp + main.g_user + "')";
                if (this.cbx_consignor_flag.Text.Trim() == "Cargo owner")
                {
                    str_user_customer = str_user_customer + str_temp;
                    try
                    {
                        if (main.dbtype == "SQL")
                        {
                            main.dbservice.Sql_exec_commandsql(str_user_customer);
                        }
                        else
                        {
                            main.dbservice.oracle_exec_commandsql(str_user_customer);
                        }
                        // btn_query_Click(sender, new EventArgs());
                    }
                    catch (System.Exception ee)
                    {
                        MessageBox.Show(main.g_class.comm_getchangestring("Fail to save", main.g_info_dataset) + "!" + ee.ToString());
                        Cursor.Current = Cursors.Default;
                        return;
                    }
                }


                str_in_sql = "insert into " + main.g_space + ".mst_customer(" +
                    "inv_org,consignor,consignor_name,short_name,english_name," +
                    "postalcode,business_address,register_address,address3,address4," +
                    "telephone,mobile_phone,fax,email,incorporation," +
                     "clerk,department,pay_way,pay_period,bank," +
                     "accounts,pay_deadline,settlement_day,appoint_day,country," +
                     "province,city,downtown,tax_no,corporation_type," +
                    "consignor_flag,customer_flag,supply_flag,putaway_rule,allocate_rule," +
                     "lotno,memo,main_dist,made_time,maker)" +
                   " values('" + main.g_org + "','" + this.tb_consignor.Text.Trim() + "','" + this.tb_consignor_name.Text.Trim() + "','" + this.tb_short_name.Text.Trim() + "','" + this.tb_english_name.Text.Trim() + "'," +
                   "'" + this.tb_postalcode.Text.Trim() + "','" + this.tb_business_address.Text.Trim() + "','" + this.tb_register_address.Text.Trim() + "','" + this.tb_address3.Text.Trim() + "','" + str_delegate_supp + "'," +
                   "'" + this.tb_telephone.Text.Trim() + "','" + this.tb_mobile_phone.Text.Trim() + "','" + this.tb_fax.Text.Trim() + "','" + this.tb_email.Text.Trim() + "','" + this.tb_incorporation.Text.Trim() + "'," +
                   "'" + this.tb_clerk.Text.Trim() + "','" + this.tb_department.Text.Trim() + "','" + this.cbx_pay_way.Text.Trim() + "','" + this.tb_pay_period.Text.Trim() + "','" + this.tb_bank.Text.Trim() + "'," +
                   "'" + this.tb_accounts.Text.Trim() + "','" + this.tb_pay_deadline.Text.Trim() + "','" + this.tb_settlement_day.Text.Trim() + "','" + this.dtp_appoint_day.Text.Trim() + "','" + this.tb_country.Text.Trim() + "'," +
                   "'" + this.tb_province.Text.Trim() + "','" + this.tb_city.Text.Trim() + "','" + str_downtown.Trim() + "','" + this.tb_tax_no.Text.Trim() + "','" + str_corporation_type.Trim() + "'," +
                   "'" + str_consignor_flag.Trim() + "','" + str_customer_flag.Trim() + "','" + str_supply_flag.Trim() + "','" + str_putawayrule + "','" + str_allocaterule + "'," +
                   "'" + str_lotno + "','" + this.tb_memo.Text.Trim() + "','A'," + str_temp;
            }
            else if (coint == 2)//修改
            {

                if (main.dbtype == "SQL")
                {
                    str_temp = str_temp + "update_time=getdate(),";
                }
                else
                {
                    str_temp = str_temp + "update_time= sysdate,";
                }
                str_temp = str_temp + "updater='" + main.g_user + "' where inv_org = '" + main.g_org + "' and consignor='" + this.tb_consignor.Text.Trim() + "'";


                str_in_sql = "update " + main.g_space + ".mst_customer set " +
                    "consignor_name='" + this.tb_consignor_name.Text.Trim() + "',short_name='" + this.tb_short_name.Text.Trim() + "',english_name='" + this.tb_english_name.Text.Trim() + "'," +
                     "postalcode='" + this.tb_postalcode.Text.Trim() + "',business_address='" + this.tb_business_address.Text.Trim() + "',register_address='" + this.tb_register_address.Text.Trim() + "',address3='" + this.tb_address3.Text.Trim() + "',address4='" + str_delegate_supp + "'," +
                     "telephone='" + this.tb_telephone.Text.Trim() + "',mobile_phone='" + this.tb_mobile_phone.Text.Trim() + "',fax='" + this.tb_fax.Text.Trim() + "',email='" + this.tb_email.Text.Trim() + "',incorporation='" + this.tb_incorporation.Text.Trim() + "'," +
                      "clerk='" + this.tb_clerk.Text.Trim() + "',department='" + this.tb_department.Text.Trim() + "',pay_way='" + this.cbx_pay_way.Text.Trim() + "',pay_period='" + this.tb_pay_period.Text.Trim() + "',bank='" + this.tb_bank.Text.Trim() + "'," +
                      "accounts='" + this.tb_accounts.Text.Trim() + "',pay_deadline='" + this.tb_pay_deadline.Text.Trim() + "',settlement_day='" + this.tb_settlement_day.Text.Trim() + "',appoint_day='" + this.dtp_appoint_day.Text.Trim() + "',country='" + this.tb_country.Text.Trim() + "'," +
                       "province='" + this.tb_province.Text.Trim() + "',city='" + this.tb_city.Text.Trim() + "',downtown='" + str_downtown.Trim() + "',tax_no='" + this.tb_tax_no.Text.Trim() + "',corporation_type='" + str_corporation_type.Trim() + "'," +
                       "consignor_flag='" + str_consignor_flag.Trim() + "',customer_flag='" + str_customer_flag.Trim() + "',supply_flag='" + str_supply_flag.Trim() + "',putaway_rule='" + str_putawayrule + "',allocate_rule='" + str_allocaterule + "'," +
                        "lotno='" + str_lotno + "',memo='" + this.tb_memo.Text.Trim() + "',main_dist='C'," + str_temp;
            }

            try
            {
                if (main.dbtype == "SQL")
                {
                    main.dbservice.Sql_exec_commandsql(str_in_sql);
                }
                else
                {
                    main.dbservice.oracle_exec_commandsql(str_in_sql);
                }

            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to save", main.g_info_dataset) + "!" + ee.ToString());
                Cursor.Current = Cursors.Default;
                return;
            }

            ClearCntrValue(this.groupBox3, false, "1");
            f_setbutton(true);
            // btn_query_Click(sender, new EventArgs());
            if (coint == 1)//添加
            {
                DataRow dr = ((DataTable)dgv_customer.DataSource).NewRow();
                dr["consignor"] = tb_consignor.Text.Trim();
                dr["consignor_name"] = tb_consignor_name.Text.Trim();
                dr["short_name"] = tb_short_name.Text.Trim();
                ((DataTable)dgv_customer.DataSource).Rows.Add(dr);
                dgv_customer.Rows[dgv_customer.CurrentRow.Index].Selected = false;
                dgv_customer.Rows[dgv_customer.Rows.Count - 1].Selected = true;
            }
        }
        private string Verification()
        {
            string _Result = "";
            string _mobile_phone = @"(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})";//手机号
            string _telephone = @"^\d{3,4}-\d{7,8}$";//电话号
            string _Number = @"^[0-9]*$";//数值
            string _email = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";//email
            if (this.tb_mobile_phone.Text.Trim() != "")
            {
                if (!Regex.IsMatch(this.tb_mobile_phone.Text.Trim(), _mobile_phone))
                {
                    this.tb_mobile_phone.Focus();
                    _Result = "mobile_phone Format Error";
                    return _Result;
                }
            }
            if (this.tb_telephone.Text.Trim() != "")
            {
                if (!Regex.IsMatch(this.tb_telephone.Text.Trim(), _telephone))
                {
                    this.tb_telephone.Focus();
                    _Result = "telephone Format Error";
                    return _Result;
                }
            }
            if (this.tb_email.Text.Trim() != "")
            {

                if (!Regex.IsMatch(this.tb_email.Text.Trim(), _email))
                {
                    this.tb_email.Focus();
                    _Result = "email Format Error";
                    return _Result;
                }
            }
            if (this.tb_pay_period.Text.Trim() != "")
            {
                if (!Regex.IsMatch(this.tb_pay_period.Text.Trim(), _Number))
                {
                    this.tb_pay_period.Focus();
                    _Result = "pay_period Format Error";
                    return _Result;
                }
            }
           
            if (this.tb_pay_deadline.Text.Trim() != "")
            {
                if (!Regex.IsMatch(this.tb_pay_deadline.Text.Trim(), _Number))
                {
                    this.tb_pay_deadline.Focus();
                    _Result = "pay_deadline Format Error";
                    return _Result;
                }
            
            }
            return _Result;
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (this.tb_consignor.Text.Trim() == "")
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Please Choose a bill which need to delete", main.g_info_dataset) + "!");
                return;
            }
            string str_temp = "";
            if (main.dbtype == "SQL")
            {
                str_temp = str_temp + "update_time=getdate(),";
            }
            else
            {
                str_temp = str_temp + "update_time= sysdate,";
            }
            if (MessageBox.Show(main.g_class.comm_getchangestring("Are you sure to delete ", main.g_info_dataset) + this.tb_consignor.Text.Trim() + "?" , "Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string str_sql = "update " + main.g_space + ".mst_customer set main_dist='D'," + str_temp + "updater='" + main.g_user + "'  where inv_org = '" + main.g_org + "' and consignor='" + this.tb_consignor.Text.Trim() + "'";
      
                try
                {
                    if (main.dbtype == "SQL")
                        main.dbservice.Sql_exec_commandsql(str_sql);
                    else
                        main.dbservice.oracle_exec_commandsql(str_sql);
                    btn_query_Click(sender, new EventArgs());
                    
                }
                catch (System.Exception ee)
                {
                    MessageBox.Show(this.tb_consignor.Text.Trim() + main.g_class.comm_getchangestring("Failed to delete", main.g_info_dataset) + "!" + ee.ToString());
                    return;
                }
            }
            //刷新货主信息
            btn_query_Click(sender, new EventArgs());
        }

        private void rb_consignor_flag_CheckedChanged(object sender, EventArgs e)
        {
            lb_consignor_ch.Text = main.g_class.comm_getchangestring("consignor", main.g_win_dataset);
           
            dscustomer = new DataSet();
            string str_common = "";
            str_common = "select a.consignor ,a.consignor_name from " + main.g_space + ".mst_customer a," + main.g_space + ".sys_user b," + main.g_space + ".sys_user_customer c" +
                                  " where b.inv_org = c.inv_org and b.user_id = c.user_id and c.inv_org = a.inv_org and c.consignor = a.consignor and b.inv_org = '"+main.g_org+"' and b.user_id = '" + main.g_user + "' and a.consignor_flag='1' and a.main_dist <> 'D' order by a.consignor";
            try
            {
                if (main.dbtype == "SQL")
                    dscustomer = main.dbservice.Sql_exec_selectsql(str_common);
                else
                    dscustomer = main.dbservice.oracle_exec_selectsql(str_common);
                cbx_customer.Text = "";
                cbx_customer.DataSource = dscustomer.Tables[0];
                cbx_customer.ValueMember = "consignor";
                cbx_customer.DisplayMember = "consignor";

                if (dscustomer.Tables[0].Rows.Count > 0) tb_cusname.Text = dscustomer.Tables[0].Rows[0][1].ToString().Trim();
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }

        }

        private void rb_customer_flag_CheckedChanged(object sender, EventArgs e)
        {
            lb_consignor_ch.Text = main.g_class.comm_getchangestring("customer_flag", main.g_win_dataset);
            dscustomer = new DataSet();
            string str_common = "";
            str_common = "select a.consignor ,a.consignor_name from " + main.g_space + ".mst_customer a" +
                      " where a.inv_org = '" + main.g_org + "' and a.customer_flag='1' and a.main_dist <> 'D' order by a.consignor";
            try
            {
                if (main.dbtype == "SQL")
                    dscustomer = main.dbservice.Sql_exec_selectsql(str_common);
                else
                    dscustomer = main.dbservice.oracle_exec_selectsql(str_common);
                cbx_customer.Text = "";
                cbx_customer.DataSource = dscustomer.Tables[0];
                cbx_customer.ValueMember = "consignor";
                cbx_customer.DisplayMember = "consignor";

                if (dscustomer.Tables[0].Rows.Count > 0) tb_cusname.Text = dscustomer.Tables[0].Rows[0][1].ToString().Trim();
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }

        }

        private void rb_supply_flag_CheckedChanged(object sender, EventArgs e)
        {
            lb_consignor_ch.Text = main.g_class.comm_getchangestring("supply_flag", main.g_win_dataset);
            dscustomer = new DataSet();
            string str_common = "";
            str_common = "select a.consignor ,a.consignor_name from " + main.g_space + ".mst_customer a" +
                                  " where a.inv_org = '" + main.g_org + "' and a.supply_flag in ('1','2') and a.main_dist <> 'D' order by a.consignor";
            try
            {
                if (main.dbtype == "SQL")
                    dscustomer = main.dbservice.Sql_exec_selectsql(str_common);
                else
                    dscustomer = main.dbservice.oracle_exec_selectsql(str_common);
                cbx_customer.Text = "";
                cbx_customer.DataSource = dscustomer.Tables[0];
                cbx_customer.ValueMember = "consignor";
                cbx_customer.DisplayMember = "consignor";
                
                if (dscustomer.Tables[0].Rows.Count > 0) tb_cusname.Text = dscustomer.Tables[0].Rows[0][1].ToString().Trim();
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }


        }

        private void cbx_customer_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_customer.Text.Trim();
            tb_cusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
           
        }

        private void dgv_customer_SelectionChanged(object sender, EventArgs e)
        {
            //DataRow[] tr = ds_transaction.Tables[0].Select("consignor='" + dgv_customer.Rows[dgv_customer.CurrentRow.Index].Cells["consignor"].Value.ToString() + "'");
            //show_all(tr[0]);
            //rb_consignor_flag_CheckedChanged(sender, new EventArgs());
        }

        private void cbx_customer_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbx_customer_Click(object sender, EventArgs e)
        {
            //rb_consignor_flag_CheckedChanged(sender, new EventArgs());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Excel文件";
            ofd.FileName = "";
            //ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//为了获取特定的系统文件夹，可以使用System.Environment类的静态方法GetFolderPath()。该方法接受一个Environment.SpecialFolder枚举，其中可以定义要返回路径的哪个系统目录
            ofd.Filter = "Excel文件(*.xls)|*.xls";
            ofd.ValidateNames = true;     //文件有效性验证ValidateNames，验证用户输入是否是一个有效的Windows文件名
            ofd.CheckFileExists = true;  //验证路径有效性
            ofd.CheckPathExists = true; //验证文件有效性

            string strName = string.Empty;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                strName = ofd.FileName;
            }

            if (strName == "")
            {
                MessageBox.Show("没有选择Excel文件！无法进行数据导入");
                return;
            }
            tb_file.Text = strName;
            GetDataFromExcelWithAppointSheetName4(strName);
        }
        /// <summary>
        /// 导入商品重量
        /// </summary>
        /// <param name="Path"></param>
        private void GetDataFromExcelWithAppointSheetName4(string Path)
        {
            //连接串
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Path + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据，并且将数字和字符混合的单元格视为文本进行导入
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();

            //返回Excel的架构，包括各个sheet表的名称,类型，创建时间和修改时间等　
            DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
            //包含excel中表名的字符串数组
            string[] strTableNames = new string[dtSheetName.Rows.Count];

            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
                //cbx_file.Items.Add(strTableNames[k].Substring(0, strTableNames[k].Length - 1));
            }
            //int i = dtSheetName.Rows.Count - 1;
            //cbx_file.Text = strTableNames[i].Substring(0, strTableNames[i].Length - 1);
            OleDbDataAdapter myCommand = null;
            DataTable dt = new DataTable();

            //从指定的表明查询数据,可先把所有表明列出来供用户选择

            string strExcel = "select*from[" + strTableNames[0] + "]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            dt = new DataTable();
            myCommand.Fill(dt);
            conn.Close();
            if (dt.Rows.Count <= 0)
            {
                return;
            }
            conn.Close();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString().Trim() == "客户" || dt.Rows[i][0].ToString().Trim() == "")
                {
                    dt.Rows.Remove(dt.Rows[i]);
                    i = i - 1;
                }
            }
            try
            {
                string str_sql = "";
                ArrayList al = new ArrayList();
                
                for (int i = 0; i < dt.Rows.Count; i++)
                {                    
                    str_sql = " update " + main.g_space + ".mst_customer  set accounts='" + dt.Rows[i]["F1"].ToString().Trim() + "' where inv_org ='" + main.g_org + "' and consignor='" + dt.Rows[i]["F1"].ToString().Trim() + "'  ";
                    al.Add(str_sql);
                }
                string[] str_sqllist = new string[al.Count];
                for (int i = 0; i < al.Count; i++)
                {
                    str_sqllist[i] = al[i].ToString().Trim();
                }


                if (main.dbtype == "SQL")
                {
                    main.dbservice.Sql_exec_mutilsql(str_sqllist);
                }
                else
                {
                    main.dbservice.oracle_exec_mutilsql(str_sqllist);
                }
                MessageBox.Show("导入成功!");
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
       
    }
}